package 上课代码;

public class BinaryTree {
    static class TreeNode {
        //孩子法,没有定义双亲
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val){
            this.val = val;
        }
    }

    public TreeNode CreatTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right=C;

        B.left=D;
        B.right=E;

        C.left=F;
        C.right=G;

        E.right=H;

        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root){
        if(root==null){
            return;//空树是不需要被遍历的
        }
        System.out.print(root.val+" ");//根
        //利用递归分别遍历左右子树
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root){
        if(root==null){
            return;//空树是不需要被遍历的
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root){
        if(root==null){
            return;//空树是不需要被遍历的
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    //求树的结点数
    public int size(TreeNode root){
        if (root==null){
            return 0;
        }
        int tmp = size(root.left)+size(root.right)+1;
        return tmp;
    }

    //获取叶子结点个数
    public int leafSize;
    public void getLeafNodeCount(TreeNode root){
        //遍历左右为空,就是叶子结点
        if(root==null){
            return;
        }
        if(root.left==null&&root.right==null){
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }

    public int getLeafNodeCount2(TreeNode root){
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            return 1;
        }
        return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);
    }

    //第k层有几个结点
    /*自写代码
    public int getLevelNodeCount(TreeNode root,int k){
        if(root==null){
            return 0;
        }
        if(root.left!=null&&root.right!=null){
            k--;
        }
        if(k==1){
            return 1;
        }
        return getLevelNodeCount(root.left,k) + getLevelNodeCount(root.right,k);
    }*/


    public int getLevelNodeCount(TreeNode root,int k){
        if(root==null){
            return 0;
        }
        if(k==1){
            return 1;
        }
        return getLevelNodeCount(root.left,k-1)+getLevelNodeCount(root.right,k-1);
    }

    //获取二叉树的高度(最大深度)
    public int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }

        int count1 = getHeight(root.left);
        int count2 = getHeight(root.right);

        return Math.max(count1,count2)+1;
    }

    //自写版
    /*public int getHeight1(TreeNode root){
        if(root==null){
            return 0;
        }
        int count1 = getHeight1(root.left)+1;
        int count2 = getHeight1(root.right)+1;
        int height = count1>count2?count1:count2;

        return height;
    }*/


    //结点是否存在(返回地址)
    public TreeNode find(TreeNode root,int val){
        if(root==null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode leftVal = find(root.left,val);
        if(leftVal!=null){
            return leftVal;
        }
        TreeNode rightVal = find(root.right,val);
        if(rightVal!=null){
            return rightVal;
        }
        return null;
    }
}
